﻿using System.Collections.Generic;

using RpcClient.Attr;
using RpcClient.Interface;
using RpcClient.Model;

using RpcModel;

using RpcModular.Domain;

namespace RpcModular.Service
{
    [UnityName("IdentityService")]
    internal class RpcIdentityService : IExtendService
    {
        private readonly IIdentityService _Service = null;

        public RpcIdentityService(IIdentityService service)
        {
            this._Service = service;
        }
        public string Name => "IdentityService";



        public void Load(IRpcService service)
        {
            service.ReceiveMsg += this.Service_ReceiveMsg;
            service.SendIng += this.Service_SendIng;
        }

        private void Service_SendIng(IRemoteConfig config, IRemote remote, object page, int sendNum)
        {
            if (this._Service.IdentityId != null)
            {
                config.SetAppIdentity(page, this._Service.IdentityId);
            }
        }



        private void Service_ReceiveMsg(IMsg msg)
        {
            if (msg.Extend != null && msg.Extend.TryGetValue("identityId", out string val))
            {
                this._Service.IdentityId = val;
            }
            if (this._Service.Check())
            {
                IdentityDomain identity = this._Service.GetIdentity();
                identity.CheckRoute(msg.MsgKey);
            }

        }
    }
}
